home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 012a / new194.zip / LISTFILE.PRG < prev    next >
Text File  |  1993-02-05  |  10KB  |  266 lines

  1. *-------------------------------------------------------------------------------
  2. *-- Program...: LISTFILE.PRG
  3. *-- Programmer: Kenneth J. Mayer (CIS: 71333,1030)
  4. *-- Date......: 01/25/1993
  5. *-- Notes.....: This program/set of routines is designed to display an ASCII
  6. *--             file of up to 1,178 lines, and 254 characters per line
  7. *--             on the screen. It allows scrolling (up,down,left,right),
  8. *--             and a few hot-keys as well:
  9. *--               <Home>        = the beginning/first character of the line
  10. *--               <End>         = the right side of a line
  11. *--               <Ctrl><Home>  = the top of the file
  12. *--               <Ctrl><End>   = the bottom of the file 
  13. *--               <PgUp>/<PgDn> = page up/down one screen at a time 
  14. *-- Usage.....: DO ListFile WITH <cFileName>,<nRow>[,<nMaxLines>[,<nTab>[,;
  15. *--                              <cColor>]]]
  16. *-- Example...: do listfile with "ListFile.PRG",5,18,3,"rg+/g"
  17. *-- Parameters: cFileName = name of file to list -- include extension and
  18. *--                         path if necessary
  19. *--             nRow      = starting row on screen (top of "window")
  20. *--             nMaxLines = optional -- number of lines to display at one
  21. *--                         time -- if left off, routine will use as manu
  22. *--                         lines as possible from nRow to bottom of screen.
  23. *--             nTab      = optional -- number of spaces to use for tab 
  24. *--                         characters at the beginning of a line. Ignores
  25. *--                         tabs after the first non-tab character in a line
  26. *--                         for speed's sake.
  27. *--             cColor    = optional -- provide color description for window,
  28. *--                         format: Foreground/Background. For example, to
  29. *--                         display the file in a window that has yellow text
  30. *--                         on a green background, the parameter would be:
  31. *--                         "rg+/g"
  32. *-------------------------------------------------------------------------------
  33.  
  34.     parameters cFileName,nRow,nMaxLines,nTab,cColor
  35.     private cWindow,cCursor,nDisplay,nBottom,nLastLine,x,nCount,nKey,;
  36.               nFirstLine,nCurrPos
  37.     
  38.     save screen to sListFile  && save screen description
  39.     cWindow = window()        && store name of any "current" window on screen
  40.     cCursor = set("CURSOR")   && save current cursor state
  41.     set cursor off            && turn it off ...
  42.     activate screen           && activate screen so we can display on TOP
  43.                               &&    of anything there.
  44.     if pCount() > 4
  45.         cColor = "COLOR "+cColor
  46.     else
  47.         cColor = ""
  48.     endif
  49.     
  50.     if type("NMAXLINES") = "L" .or. isblank(nMaxLines) 
  51.                                      && determine # of lines to display on screen ...
  52.         *-- find bottom of screen, and then subtract nRow from that ...
  53.         if set("DISPLAY") # "MONO"
  54.             nDisplay = val(right(set("DISPLAY"),2))
  55.         else
  56.             nDisplay = 24
  57.         endif
  58.         if set("STATUS") = "ON"    && if status line is on, we have two less
  59.                                    && lines to work with
  60.             nDisplay = nDisplay - 4
  61.         endif
  62.         nMaxLines = nDisplay - nRow
  63.     endif
  64.     
  65.     *-- bottom row of window is based on nMaxLines
  66.     nBottom = nRow + nMaxLines
  67.     
  68.     if type("NTAB") = "L" && set default ... notice that if it's 0, that's
  69.                           && not undefined
  70.         nTab = 5
  71.     endif
  72.     
  73.     *-- display a message for user to let them know we haven't just
  74.     *-- disappeared ...
  75.     @10,27 to 12,51 double color rg+/gb
  76.     @11,28 say "Reading/Processing File" color rg+/gb
  77.     
  78.     *-- get it
  79.     nLastLine = TextLine(cFileName)   && obtain line number of last line of file
  80.     x = AAppend(cFileName,"aFileList")  && put file into array
  81.     
  82.     *-- deal with tabs here
  83.     if nTab # 0
  84.         nCount = 1
  85.         do while nCount < nLastLine
  86.             do while chr(9) $ aFileList[nCount]   && loop while current character is a tab
  87.                 aFileList[nCount] = ;
  88.                     stuff(aFileList[nCount],at(chr(9),aFileList[nCount]),1,space(nTab))
  89.             enddo
  90.             nCount = nCount + 1
  91.         enddo
  92.     endif
  93.     
  94.     *-- loop and pad each array element with spaces to 254 characters
  95.     nCount = 1
  96.     do while nCount < nLastLine
  97.         aFileList[nCount] = aFileList[nCount]+space(254-len(aFileList[nCount]))
  98.         nCount = nCount + 1
  99.     enddo
  100.     
  101.     *-- remove message
  102.     restore screen from sListFile
  103.     
  104.     *-- define window
  105.     define window wListFile from nRow,0 to nBottom,79 none &cColor.
  106.     activate window wListFile
  107.     
  108.     *-- now that we're here, let's go ...
  109.     nKey = 0         && initialize to something we're not looking for
  110.     nFirstLine = 1   && First line to display out of list ...
  111.     nCurrPos   = 1   && current position in string
  112.     
  113.     *-- here's the actual loop ...
  114.     do while nKey # 27   && must press <Esc> to exit
  115.         
  116.         *-- display loop
  117.         nCounter = 0
  118.         do while nCounter < nMaxLines
  119.         
  120.             @nCounter,0 say substr(aFileList[nFirstLine+nCounter],nCurrPos,80)
  121.             nCounter = nCounter + 1
  122.         
  123.         enddo
  124.         
  125.         *-- get keypress
  126.         nKey = inkey(0)   && wait for a keypress
  127.         
  128.         *-- if keypress is one of the following, do something with it ...
  129.         do case
  130.             case nKey = 5    && up arrow  = up one row
  131.                 if nFirstLine > 1
  132.                     nFirstLine = nFirstLine - 1
  133.                 endif
  134.             case nKey = 24   && down arrow = down one row
  135.                 if nFirstLine+nMaxLines < nLastLine
  136.                     nFirstLine = nFirstLine + 1
  137.                 endif
  138.             case nKey = 3    && <PgDn>  = down one screen
  139.                 if nFirstLine+nMaxLines < nLastLine - nMaxLines
  140.                     nFirstLine = nFirstLine + nMaxLines
  141.                 else
  142.                     nFirstLine = nLastLine - nMaxLines
  143.                 endif
  144.             case nKey = 18   && <PgDn>  = up one screen
  145.                 if nFirstLine - nMaxLines > 1
  146.                     nFirstLine = nFirstLine - nMaxLines
  147.                 else
  148.                     nFirstLine = 1
  149.                 endif
  150.             case nKey = 23   && <Ctrl><End>   = End of File
  151.                 nFirstLine = nLastLine - nMaxLines
  152.             case nKey = 29   && <Ctrl><Home>  = Beginning of File
  153.                 nFirstLine = 1
  154.             case nKey = 19   && <Left> = Back up one character
  155.                 if nCurrPos > 1
  156.                     nCurrPos = nCurrPos - 1
  157.                 endif
  158.             case nKey = 4    && <Right> = Go RIGHT one character
  159.                 if nCurrPos < 174  && 254-80 (width of string - screen width
  160.                     nCurrPos = nCurrPos + 1
  161.                 endif
  162.             case nKey = 2    && <End> = end of line
  163.                 nCurrPos = 174   && show last character(s) on right side of text
  164.             case nKey = 26   && <Home> = beginning of line
  165.                 nCurrPos = 1
  166.         endcase
  167.         
  168.     enddo
  169.     
  170.     *-- if here, we <Esc>aped out of the loop
  171.     deactivate window wListFile
  172.     release window wListFile
  173.     restore screen from sListFile
  174.     release screen sListFile
  175.     if .not. isblank(cWindow)
  176.         activate window &cWindow
  177.     endif
  178.     release aFileList
  179.     set cursor &cCursor
  180.     
  181. RETURN
  182. *-- EoP: ListFile
  183.  
  184. FUNCTION AAppend
  185. *-------------------------------------------------------------------------------
  186. *-- Programmer..: Adam L. Menkes (Borland Technical Support)
  187. *-- Date........: 04/xx/1992
  188. *-- Notes.......: Appends a text file into an array. This routine is limited to
  189. *--               text files of 1,170 lines, and 254 characters per line.
  190. *--               The text file must be an ASCII Txt formatted file. Taken from
  191. *--               Technotes, April, 1992.
  192. *-- Written for.: dBASE IV, 1.5
  193. *-- Rev. History: None
  194. *-- Calls.......: TextLine()           Function in LOWLEVEL.PRG
  195. *-- Called by...: Any
  196. *-- Usage.......: AAppend(<cFileName>,<aArrayName>)
  197. *-- Example.....: ?AAppend("CONFIG.DB","aConfig")
  198. *-- Returns.....: .T.
  199. *-- Parameters..: cFileName  = Name of DOS Text file to read into array
  200. *--               aArrayName = Name of array to create. If it already exists,
  201. *--                            this array will be destroyed and overwritten.
  202. *-------------------------------------------------------------------------------
  203.  
  204.    parameters cFileName, aArrayName
  205.    private aTArray, nLines, nX, nHandle
  206.  
  207.    *-- assign array name to a temp variable name ...
  208.    aTArray = aArrayName
  209.    *-- if it exists, get rid of it, and then re-define it
  210.    release &aTArray
  211.    public  &aTArray
  212.    nLines = TextLine(cFileName)  && get number of lines
  213.    declare &aTArray[min(nLines,1170)]
  214.  
  215.    *-- get file handle
  216.    nHandle = fopen(cFileName)
  217.  
  218.    *-- store the file into the array
  219.    nX = 1
  220.    do while nX <= nLines
  221.       store fgets(nHandle,254) to &aTArray[nX]
  222.       nX = nX + 1
  223.    enddo
  224.  
  225.    *-- close the file
  226.    nHandle = fClose(nHandle)
  227.  
  228. RETURN .T.
  229. *-- EoF: AAppend()
  230.  
  231. FUNCTION TextLine
  232. *-------------------------------------------------------------------------------
  233. *-- Programmer..: Adam L. Menkes (Borland Technical Support)
  234. *-- Date........: 04/xx/1992
  235. *-- Notes.......: Returns the number of lines of text in an ASCII Text File
  236. *--               Taken from TechNotes, April, 1992
  237. *-- Written for.: dBASE IV, 1.5
  238. *-- Rev. History: None
  239. *-- Calls.......: None
  240. *-- Called by...: Any
  241. *-- Usage.......: TextLine(<cTextFile>)
  242. *-- Example.....: ?TextLine("CONFIG.DB")
  243. *-- Returns.....: Number of lines
  244. *-- Parameters..: cTextFile = name of file
  245. *-------------------------------------------------------------------------------
  246.  
  247.    parameter cTextFile
  248.    private nLines, nHandle, cTemp, nClose
  249.  
  250.    nLines = 0
  251.    if file(cTextFile)   && if it exists ...
  252.       nHandle = fopen(cTextFile,"R")
  253.       do while .not. feof(nHandle)
  254.      cTemp = fgets(nHandle,254)
  255.      nLines = nLines + 1
  256.       enddo
  257.       nClose = fclose(nHandle)
  258.    endif
  259.  
  260. RETURN nLines
  261. *-- EoF: TextLine()
  262.  
  263. *-------------------------------------------------------------------------------
  264. *-- End of Program: LISTFILE.PRG
  265. *-------------------------------------------------------------------------------
  266.